#!/usr/bin/python
# ircbotmini.py
# Copyright (C) Vincenzo Tilotta 2010 <tailot@gmail.com>
#
# ircbotmini.py is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ircbotmini.py is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
import socket,sys,time,threading,random
#configuration
CHAN = ["#9w9.org"]
PASSWORD = '123456'
FLAGSPAM = True
DEBUG = True
MSGSPAM = 'Ritorno fra 2 minuti, visita il mio sito www.9w9.org, fammi sapere se ti piace. A dopo\r\n'
#end configuration
try:
	SERVER = str(sys.argv[1])
	PORT = int(sys.argv[2])
	NAME = str(sys.argv[3])
except:
	print "Example: python myb.py SERVER_IRC PORT NICKNAME\n"
	sys.exit()
def clone():
	random.seed()
	myname = "B"+str(random.randint(10000, 99999))
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect((SERVER, PORT))
	s.send ("NICK %s\r\n" % (myname))
	s.send("USER "+myname+" %s bla :%s\r\n" % (SERVER,myname))
	for i in CHAN:
		s.send ("JOIN %s\r\n" % (i))
	while 1:
		ricev = s.recv(1024)
		if ricev.startswith ('PING '):
			s.send('PONG :' + ricev.split(' ')[1] + '\r\n')
		if ricev.find('PRIVMSG '+myname) != -1:
			mysplit = ricev.split("!")
			if mysplit[1].find(PASSWORD) != -1:
				if mysplit[1].find('nick') != -1:
					mynick = mysplit[1].split(myname + " :")[1].replace(PASSWORD+" nick ","")
					s.send ("NICK %s\r\n" % (mynick))
		time.sleep(0.5)
def connection():
	global FLAGSPAM, NAME
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect((SERVER, PORT))
	s.send ("NICK %s\r\n" % (NAME))
	s.send("USER "+NAME+" %s bla :%s\r\n" % (SERVER,NAME))
	for i in CHAN:
		s.send ("JOIN %s\r\n" % (i))
	while 1:
		ricev = s.recv(1024)
		#print ricev
		if ricev.startswith ('PING '):
			s.send('PONG :' + ricev.split(' ')[1] + '\r\n')
		if ricev.find('PRIVMSG '+NAME) != -1:
			mysplit = ricev.split("!")
			victim = mysplit[0].replace(":","")
			if DEBUG:
				print victim
			if mysplit[1].find(PASSWORD) != -1:
				if mysplit[1].find('#') != -1:
					myjoin = mysplit[1].split(NAME + " :")[1].replace(PASSWORD+" ","")
					s.send ("JOIN %s\r\n" % (myjoin))
					CHAN.append(myjoin)
				elif mysplit[1].find('clone') != -1:
					thread1 = threading.Thread( target=clone )
					thread1.start()
				elif mysplit[1].find('spam') != -1:
					if FLAGSPAM:
						FLAGSPAM = False
						s.send('PRIVMSG '+victim+ ' :SPAM - False\r\n')
					else:
						FLAGSPAM = True
						s.send('PRIVMSG '+victim+ ' :SPAM - True\r\n')
			else:
				if FLAGSPAM:
					s.send('PRIVMSG '+victim+ ' :'+MSGSPAM)
		time.sleep(0.2)
if __name__ == "__main__":
    sys.exit(connection())
